#include <cstdlib>
#include <cstdio>
#include <iostream>
#include "d_Math.h"

using namespace d_Math;

d_Vecf::d_Vecf()
{
	vec_size = -1;

}

d_Vecf::d_Vecf(int n)
{
	set_size(n);
}

d_Vecf d_Vecf::get_part(int start, int end)
{
	d_Vecf result(end-start+1);
	for(int i=0; i<end-start+1; i++)
		result[i] = p[i+start];
	return result;
}

void d_Vecf::set_part(int start, d_Vecf part)
{
	for(int i=0; i<part.get_size(); i++)
		if(start+i<get_size())
			p[i+start] = part[i];
}

void d_Vecf::set_size(int size)
{
	if(vec_size == -1)
	{
		p = (float *) malloc (size*sizeof(float));
		vec_size = size;
		for(int i=0; i<size;i++)
			p[i]=0;
		return;
	}
	else
	{
		float *tmp = (float *) malloc (size*sizeof(float));
		int iter=0;
		if(size > vec_size)
			iter = vec_size;
		else
			iter = size;
		for(int i=0; i<iter; i++)
		{
			tmp[i] = p[i];
		}
		p = tmp;
		vec_size = size;
	}
}

d_Vecf d_Vecf::operator + (d_Vecf a)
{
	d_Vecf aux;
	aux.set_size(vec_size);
	for(int i=0; i<vec_size; i++)
		aux[i] = p[i] + a[i];
	return aux;
}

d_Vecf d_Vecf::operator - (d_Vecf a)
{
	d_Vecf aux;
	aux.set_size(vec_size);
	for(int i=0; i<vec_size; i++)
		aux[i] = p[i] - a[i];
	return aux;
}

float d_Vecf::operator * (d_Vecf a)
{
	float res=0;
	for(int i=0; i<vec_size; i++)
		res += p[i]*a[i];
	return res;
}

d_Vecf d_Vecf::operator * (float a)
{
	d_Vecf aux;
	aux.set_size(vec_size);
	for(int i=0; i<vec_size; i++)
		aux[i] = p[i] * a;
	return aux;
}

d_Vecf d_Vecf::operator * (d_Matf m)
{
	d_Vecf res;
	res.set_size(m.n);
	for(int j=0; j<m.n; j++)
		for(int i=0; i<m.m; i++)
		{
			res[j]+=p[i]*m(i,j);
		}
	return res;
}

d_Vecf d_Vecf::operator / (float a)
{
	d_Vecf aux;
	aux.set_size(vec_size);
	for(int i=0; i<vec_size; i++)
		aux[i] = p[i] / a;
	return aux;
}

d_Vecf d_Vecf::operator + (void) const
{
	return *this;
}

d_Vecf d_Vecf::operator - (void) const
{
	d_Vecf aux;
	aux.set_size(vec_size);
	for(int i=0; i<vec_size; i++)
		aux[i] = -p[i];
	return aux;
}

void d_Vecf::print()
{
	std::cout << "[ ";
	for(int i=0; i<vec_size; i++)
		std::cout << p[i] << " ";
	std::cout << "]" << std::endl;
}


